package com.drpclub.gamebackend.controller;

import com.drpclub.gamebackend.bean.User;
import com.drpclub.gamebackend.common.constant.ActionUri;
import com.drpclub.gamebackend.common.constant.RootUri;
import com.drpclub.gamebackend.common.utils.DrpUtils;
import com.drpclub.gamebackend.common.utils.NetworkResult;
import com.drpclub.gamebackend.common.utils.log.RsHelper;
import com.drpclub.gamebackend.service.UserService;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bind.annotation.Empty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.Callable;

import static com.drpclub.gamebackend.common.utils.DrpUtils.getToken;


/**
 * @author dongruipeng
 * @Descrpition
 * @date 2019year 12month04day  14:40:18
 */
@RequestMapping(RootUri.USER)
@RestController
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "register")
    @PostMapping(ActionUri.SAVE)
    public Callable<NetworkResult<Object>> userRegister(@RequestBody @Valid User user) {
        return () -> RsHelper.success (userService.register (user));
    }

    @ApiOperation(value = "login")
    @GetMapping(ActionUri.LOGIN)
    public Callable<NetworkResult<Object>> userLogin(@RequestParam @Empty String username,
                                                     @RequestParam @Empty String password,
                                                     @RequestParam @Empty String validCode) {
        return () -> RsHelper.success (userService.login (username, password, validCode));
    }

    @ApiOperation(value = "valid code")
    @GetMapping(ActionUri.VALID_CODE)
    public Callable<NetworkResult<Object>> getValidCode(@RequestParam @Empty String username) {
        return () -> RsHelper.success (userService.generateValidCode (username));
    }

    @ApiOperation(value = "change password need auth")
    @PutMapping(ActionUri.CHANGE_PASSWORD)
    public Callable<NetworkResult<Object>> changePassword(@RequestParam @Empty String oldPassword,
                                                          @RequestParam @Empty String newPassword,
                                                          HttpServletRequest request) {
        return () -> RsHelper.success (userService.changePassword (getToken (request), oldPassword, newPassword));
    }

    @ApiOperation(value = "change headImage need auth")
    @PostMapping(ActionUri.CHANGE_HEAD_IMAGE)
    public Callable<NetworkResult<Object>> changeHeadImage(@RequestParam MultipartFile headImage,
                                                           HttpServletRequest request) {
        return () -> RsHelper.success (userService.changeHeadImage (headImage, getToken (request)));
    }

    @ApiOperation (value = "change nickname")
    @PutMapping(ActionUri.CHANGE_NICKNAME)
    public Callable<NetworkResult<Object>> changeNickname(@RequestParam @Empty String nickname, HttpServletRequest request) {
        return () -> RsHelper.success (userService.changeNickname(nickname, getToken (request)));
    }

    @ApiOperation (value = "get User info")
    @GetMapping(ActionUri.GET_USER_INFO)
    public Callable<NetworkResult<Object>> getUserInfo(HttpServletRequest request) {
        return () -> RsHelper.success (userService.getUserInfo(DrpUtils.getToken (request)));
    }
}
